package com.mvc.frame.sys;



import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;


public class CustomExceptionResolver implements HandlerExceptionResolver
{

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e)
	{
		logger.error("异常捕获：", e);
         /*  使用response返回    */  
         response.setStatus(org.springframework.http.HttpStatus.OK.value()); //设置状态码  
         response.setContentType(MediaType.APPLICATION_JSON_VALUE); //设置ContentType  
         response.setCharacterEncoding(SystemConfiger.systemEcoding); //避免乱码  
         response.setHeader("Cache-Control", "no-cache, must-revalidate");  
         try 
         {  
        	 AjaxResult result=new AjaxResult();
        	 Object json=result.ajaxResultFailure(e);
             response.getWriter().write(JSON.toJSONString(json));  
         } 
         catch (IOException ex) 
         {  
            logger.error("与客户端通讯异常:"+ ex.getMessage(), ex);  
         }  
		return null;
	}

	




	
}
